package com.example.actearly_revised;

//import titanic.swim.team.SQLTask.DbHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLAnswers {

	public static final String KEY_ROW = "_id";
	public static final String DATABASE_NAME = "Questions";
	public static final String A1 = "A1";
	public static final String A2 = "A2";
	private static final String A3 = "A3";
	private static final String DATABASE_TABLE = "Questions";
	private static final String A4 = "A4";
	private static final String A5 = "A5";
	private static final String  A6 = "A6";
	private static final String A7 = "A6";
	private static final String A8 = "A7";
	private static final String A9 = "A8";
	private static final String A10 = "A9";
	private static final int DATABASE_NUM = 1;
	//public HashSet newHashMap;
	private static DbHelper ourHelper;
	private final  Context ourContext;
	private static SQLiteDatabase ourDatabase;

	/**
	 * this is the DBhelper class
	 * @author stephengadd
	 *
	 */
	private class DbHelper extends SQLiteOpenHelper{
		public DbHelper(Context context){
			super(context, DATABASE_NAME, null, DATABASE_NUM );
		}
		/**
		 * this is the onCreate method that creates the database
		 * this method is only called if there isn't a database
		 */
		@Override
		public void onCreate(SQLiteDatabase db){
			Log.d("on create ","Oncreate");
			db.execSQL("CREATE TABLE \'" + DATABASE_TABLE + "\' ( \'"+
					KEY_ROW + "\' INTEGER PRIMARY KEY AUTOINCREMENT, \'" 
					+ A1  + "\' TEXT NOT NULL, \'"
					+ A2 + "\' TEXT NOT NULL, \'"
					+ A3 + "\' TEXT NOT NULL, \'"
					+ A4 + "\' TEXT NOT NULL, \'"
					+ A5 + "\' TEXT NOT NULL, \'"
					+ A6 + "\' TEXT NOT NULL, \'"
					+ A7 + "\' TEXT NOT NULL, \'" +
					A8 + "\' TEXT NOT NULL, \'" +
					A9 + "\' TEXT NOT NULL);"
);
		}

		/**
		 * this is onUpgrade method that deletes the old database and adds the new database
		 */
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion ){
			db.execSQL("DROP TABLE IF EXIST " + DATABASE_TABLE);
			onCreate(db);
		}


	}

	/**
	 * the constructer for the SQL database
	 * @param c
	 */
	public SQLAnswers(Context c){
		ourContext = c;
	}

	/**
	 * this method opens the database connection
	 * @throws SQLException
	 */
	public void open() throws SQLException{
		Log.d("Sql Conector", "ready to open");
		ourHelper = new DbHelper(ourContext);
		Log.d("Sql Connector", "made helper");

		ourDatabase = ourHelper.getWritableDatabase();
		Log.d("Sql Connector", "returning after making db");
	}

	/**
	 * this method closes the database connection
	 */
	public void close(){
		ourHelper.close();
	}

	/**
	 * this method creates the entry for the databae and returns a long indicating it finalized
	 * @param name
	 * @param email
	 * @param user
	 * @param password
	 * @return
	 */
	public long createAnswer(String question, String answer){
		ContentValues cv = new ContentValues();
		cv.put(question, answer);
		//cv.put(A2, "");
		//cv.put(A3, "");
		//cv.put(A4, "");
		//cv.put(A5, "");
		//cv.put(A6, "");
		//cv.put(A7, "");
		//cv.put(A8, "");
		//cv.put(A9, "");
		return ourDatabase.insert(DATABASE_TABLE, null, cv);

	}

	/**
	 * this is the update user function that updates the user with the proper data
	 * @param userName
	 * @param name
	 * @param email
	 * @param password
	 */
	public void updateUser(String userName, String name, String email, String password){
	}

	/**
	 * this function returns information that is already in the database
	 * @return User
	 */
	/*public List<Task> getData(){
		String[] columns = new String[]{ KEY_ROW, USER_NAME, NAME, DATE, TIME, TASK_DESCRIPTION, CATEGORY, GPS, RECURRING, COMPLETED };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
		String result = "";
		List<Task> newList = new LinkedList<Task>();
		Task newTask =  null;
		int iRow = c.getColumnIndex(KEY_ROW);
		int iUserName = c.getColumnIndex(USER_NAME);
		int iDate = c.getColumnIndex(DATE);
		int iTime = c.getColumnIndex(TIME);
		int iName = c.getColumnIndex(NAME);
		int iTask_Description = c.getColumnIndex(TASK_DESCRIPTION);
		int iCategory = c.getColumnIndex(TASK_DESCRIPTION);
		int iRecurring = c.getColumnIndex(RECURRING);
		int iGPS = c.getColumnIndex(GPS);
		int iCompleted = c.getColumnIndex(COMPLETED);

		for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
			result = result + "Row" + c.getString(iRow) + " UserName "  + c.getString(iUserName) + " Password " +
					c.getString(iName) + " Date " + c.getString(iDate) + " Time " + c.getString(iTime) + " Task Description " +  c.getString(iTask_Description) + " Category " + c.getString(iCategory) +" \n";	
			newTask = new Task(c.getString(iUserName), c.getString(iDate), c.getString(iTime), c.getString(iName), c.getString(iTask_Description) , c.getString(iCategory), c.getString(iRecurring), c.getString(iGPS));
			if (newTask != null){
			newList.add(newTask);
			}
		}
		System.out.println(result);
	
		return newList;

	}*/
}
